package talk.Net;


import com.alibaba.fastjson.JSON;
import talk.Bean.MyUser;
import talk.Bean.Register;
import talk.Bean.Token;
import talk.Bean.addFriend;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by debby on 2018/3/27.
 * 描述:网络请求
 */

public class HttpRequest {

    /**
     * 获取token
     *
     * @param userId
     * @param name
     * @param portraitUri
     * @param observe
     */
    public static void getToken(String userId, final String name, String portraitUri, Observer<Token> observe) {
        RetrofitUtil.getApisService().getToken(userId, name, portraitUri)
                .subscribeOn(Schedulers.io())
                .map(new Function<String, Token>() {
                    @Override
                    public Token apply(String s) throws Exception {
                        return JSON.parseObject(s, Token.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observe);
    }


    /**
     * 登陆
     *
     * @param name
     * @param password
     * @param observe
     */

    public static void login(String name, String password, Observer<MyUser> observe) {
        RetrofitUtil.getApisService().login(name, password)
                .subscribeOn(Schedulers.io())
                .map(new Function<String, MyUser>() {
                    @Override
                    public MyUser apply(String s) throws Exception {
                        return JSON.parseObject(s, MyUser.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observe);
    }

    public static void addFriend(String uId, String friendId, Observer<addFriend> observe) {
        RetrofitUtil.getApisService().addFriend(uId, friendId)
                .subscribeOn(Schedulers.io())
                .map(new Function<String, addFriend>() {
                    @Override
                    public addFriend apply(String s) throws Exception {
                        return JSON.parseObject(s, addFriend.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observe);
    }

    public static void register(String name, String password,
                                String email, String imgurl, Observer<Register> observe) {
        RetrofitUtil.getApisService().register(name, password, email, imgurl)
                .subscribeOn(Schedulers.io())
                .map(new Function<String, Register>() {
                    @Override
                    public Register apply(String s) throws Exception {
                        return JSON.parseObject(s, Register.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observe);
    }

    public static void findUser(String uId, Observer<MyUser> observe) {
        RetrofitUtil.getApisService().findUser(uId)
                .subscribeOn(Schedulers.io())
                .map(new Function<String, MyUser>() {
                    @Override
                    public MyUser apply(String s) throws Exception {
                        return JSON.parseObject(s, MyUser.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observe);
    }


}
